//
//  ADSuyiCustomAdapterTestUnifiedNativeAdView.m
//  ADSuyiCustomPlatform
//
//  Created by Erik on 2021/9/7.
//

#import "ADSuyiCustomAdapterTestUnifiedNativeAdView.h"

@interface ADSuyiCustomAdapterTestUnifiedNativeAdView ()
{
    ADSuyiAdapterNativeAdData *_data;
    CGFloat _width;
}
@end

@implementation ADSuyiCustomAdapterTestUnifiedNativeAdView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _adView = [GDTUnifiedNativeAdView new];
//        _adView.backgroundColor = UIColor.redColor;
        [self addSubview:_adView];
    }
    return self;
}

#pragma mark - ADSuyiAdapterNativeAdViewDelegate

- (void)adsy_registViews:(NSArray<UIView *> *)clickViews {
    [self.adView registerDataObject:_adData clickableViews:clickViews];
    if([self.delegate respondsToSelector:@selector(adsyCustomUnifiedNativeAdViewRender:)]) {
        [self.delegate adsyCustomUnifiedNativeAdViewRender:self];
    }
}



- (ADSuyiAdapterNativeAdData *)data {
    return _data;
}

- (nullable UIView *)adsy_mediaViewForWidth:(CGFloat)width {
    [self.adView.mediaView muteEnable:YES];
    if (width == 0.0)
        width = self.adView.mediaView.bounds.size.width;
    self.adView.mediaView.frame = CGRectMake(self.adView.mediaView.frame.origin.x, self.adView.mediaView.frame.origin.y, width, width*9/16);
    _width = width;
    return self.adView.mediaView;
}

- (void)setMediaView:(GDTMediaView *)mediaView {
    _mediaView = mediaView;
}

- (void)adsy_platformLogoImageDarkMode:(BOOL)darkMode loadImageBlock:(void (^)(UIImage * _Nullable))block {
    if (!block) {
        return;
    }
    block(nil);
}

- (ADSuyiAdapterRenderType)renderType {
    return ADSuyiAdapterRenderTypeNative;
}

- (void)adsy_unRegistView {
    [self.adView unregisterDataObject];
}

- (void)adsy_close {
    [self.delegate adsyCustomUnifiedNativeAdViewClose:self];
}

- (BOOL)adsy_closeButtonExist {
    return NO;
}

#pragma mark - Set

- (void)setAdData:(GDTUnifiedNativeAdDataObject *)adData {
    _adData = adData;
//    [self.adView registerDataObject:_adData clickableViews:@[self]];
    _data = [self getNativeDataWithTitle:_adData.title content:_adData.desc imageUrlStringArray:@[_adData.imageUrl] iconImage:nil iconImageUrl:_adData.iconUrl shouldShowMediaView:_adData.isVideoAd];
}


#pragma mark - ADSuyiAdViewInfoProtocol

- (ADSuyiAdapterPlatform)adsy_platform {
    return @"gdt";
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _adView.frame = self.bounds;
    _adView.mediaView.frame = CGRectMake(self.adView.mediaView.frame.origin.x, self.adView.mediaView.frame.origin.y, _width, _width*9/16);
}

@end
